/*
 * Copyright(c) 2018-2018, YuanYue Group, Inc. All rights reserved.
 *
 * This software is the confidential and proprietary information of YuanYue Group, Inc.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with YuanYue Group.
 */
package net.wsfcp.android.ui.view.my.fragment;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import net.wsfcp.android.R;
import net.wsfcp.android.base.BaseFragment;
import net.wsfcp.android.bean.resultbean.UserBean;
import net.wsfcp.android.greendao.utils.UserBeanUtil;
import net.wsfcp.android.http.mvp.contract.RegisterContract;
import net.wsfcp.android.event.LoginEvent;
import net.wsfcp.android.http.mvp.presenter.RegisterPresenter;
import net.wsfcp.android.utils.CommonUtils;
import net.wsfcp.android.utils.InputStreamUtils;
import net.wsfcp.android.utils.SpUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;

/**
 * LoginActivity.java是全网覆盖平台的。
 *
 * @author admin
 * @version $Id: LoginActivity.java, v 0.1 2018-12-14 16:45 admin Exp $$
 */
//登陆页面子页面-注册页面
public class RegisterFragment extends BaseFragment<RegisterPresenter> implements RegisterContract.RegisterView {
    @BindView(R.id.txt_login)
    TextView txtLogin;
    @BindView(R.id.edt_username)
    EditText edtUsername;
    @BindView(R.id.edt_password)
    EditText edtPassword;
    @BindView(R.id.edt_password_again)
    EditText edtPasswordAgain;
    @BindView(R.id.edt_mail)
    EditText edtMail;
    @BindView(R.id.edt_code)
    EditText edtCode;
    @BindView(R.id.img_code)
    ImageView imgCode;

    //登陆页面和注册页面都会去请求图形验证码，因此切换的时候要重新获取当前页面验证码
    public void getImgCode() {
        if (mPresenter == null)
            mPresenter = new RegisterPresenter();
        mPresenter.getImageCode();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_register;
    }


    @Override
    protected RegisterPresenter loadPresenter() {
        if (mPresenter == null) {
            return new RegisterPresenter();
        } else {
            return mPresenter;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        txtLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mPresenter.register(getUserName(), getPwd(), getMail(), getCode());
            }
        });

        imgCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getImageCode();
            }
        });
    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected void initView() {

    }


    @Override
    public String getUserName() {
        return edtUsername.getText().toString().trim();

    }

    @Override
    public String getPwd() {
        return edtPassword.getText().toString().trim();

    }

    @Override
    public String getPwdAgain() {
        return edtPasswordAgain.getText().toString().trim();

    }

    @Override
    public String getMail() {
        return edtMail.getText().toString().trim();

    }

    @Override
    public String getCode() {
        return edtCode.getText().toString().trim();
    }


    @Override
    public void getImageCodeSuccess(InputStream inputStream) throws IOException {
        Glide.with(this).load(InputStreamUtils.input2byte(inputStream)).apply(new RequestOptions().placeholder(R.mipmap.ic_launcher).centerInside()).into(imgCode);
    }

    @Override
    public void getImageCodeFail(String failMsg) {

    }


    public boolean checkNull() {
        boolean isNull = false;
        if (edtUsername.getText().toString().trim().length() == 0) {
            toast("用户名不能为空");
            isNull = true;
            dismissPg();
        } else if (edtPassword.getText().toString().trim().length() == 0) {
            toast("密码不能为空");
            isNull = true;
            dismissPg();
        } else if (edtPasswordAgain.getText().toString().trim().length() == 0) {
            toast("确认密码不能为空");
            isNull = true;
            dismissPg();
        } else if (edtMail.getText().toString().trim().length() == 0) {
            toast("邮箱不能为空");
            isNull = true;
            dismissPg();
        } else if (edtCode.getText().toString().trim().length() == 0) {
            toast("验证码不能为空");
            isNull = true;
            dismissPg();
        } else if (!CommonUtils.isEmail(edtMail.getText().toString().trim())) {
            toast("邮箱格式错误");
            isNull = true;
            dismissPg();
        } else if (!edtPassword.getText().toString().trim().equals(edtPasswordAgain.getText().toString().trim())) {
            toast("两次输入的密码不一致");
            isNull = true;
            dismissPg();
        }
        return isNull;
    }

    @Override
    public void registerSuccess(UserBean loginBean, String pwd) {

        UserBeanUtil userBeanUtil = new UserBeanUtil(getContext());
        userBeanUtil.updateUser(loginBean);

        SpUtils.getInstance().saveUserInfo(loginBean);
        SpUtils.getInstance().put(SpUtils.PASSWORD, pwd);
        SpUtils.getInstance().put(SpUtils.LOGIN_STATE, 1);
        EventBus.getDefault().post(new LoginEvent(1));
        toast("注册成功");

    }

    @Override
    public void registerFail(String failMsg) {
        toast(failMsg);
    }


//    //接收订阅的事件
//    @Subscribe(threadMode = ThreadMode.POSTING)
//    public void login(LoginEvent event) {
//        switch (event.getCode()) {
//            case 0:   // 退出登录
//                finish();
//                break;
//            case 1:   // 登录
//                finish();
//                break;
//            case 2:   // 忘记密码
//                finish();
//                break;
//
//        }
//
//    }
}